package com.zscat.mallplus.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.cms.entity.CmsFavorite;
import com.zscat.mallplus.cms.service.ICmsFavoriteService;
import com.zscat.mallplus.pms.service.IPmsFavoriteService;
import com.zscat.mallplus.ums.entity.UmsCollect;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsCollectService;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 会员收藏管理
 */
@RestController
@Api(tags = "MemberCollectionController", description = "会员收藏和点赞管理")
@RequestMapping("/api/collection")
public class MemberCollectionController {
	@Autowired
	private IPmsFavoriteService memberCollectionService;
	@Autowired
	private ICmsFavoriteService cmsFavoriteService;

    @Resource
    private ICmsFavoriteService favoriteService;

    @Resource
    private IUmsMemberService memberService;

    @Resource
	private IUmsCollectService collectService;


    @ApiOperation("商品或文章收藏")
    @PostMapping(value = "collect")
    public Object collect(UmsCollect collect) {
        return collectService.saveCollect(collect);
    }

    @ApiOperation("取消收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品或文章ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "收藏类别：1->商品；2-4>活动商品；5->科普;6->活动；7->研学；8->创课；", required = true, dataType = "int", paramType = "query")
    })
    @PostMapping(value = "cancelCollect")
    public Object cancelCollect(@RequestParam(value = "productId", required = true) Long productId,
                                @RequestParam(value = "type", required = true) Integer type) {
        if (ValidatorUtils.empty(productId)) {
            return new CommonResult().failed("商品或文章ID不能为空");
        }
        if (ValidatorUtils.empty(type)) {
            return new CommonResult().failed("收藏类别不能为空");
        }
        return collectService.cancelCollect(productId, type);
    }

//    @ApiOperation("删除收藏中的某个商品")
//    @PostMapping(value = "/delete")
//    public Object delete(String ids) {
//        if (StringUtils.isEmpty(ids)) {
//            return new CommonResult().failed("参数为空");
//        }
//        List<Long> resultList = new ArrayList<>(ids.split(",").length);
//        for (String s : ids.split(",")) {
//            resultList.add(Long.valueOf(s));
//        }
//        if (memberCollectionService.removeByIds(resultList)) {
//            return new CommonResult().success();
//        }
//        return new CommonResult().failed();
//    }

//    @ApiOperation("显示收藏列表")
//    @GetMapping(value = "/listCollectByType")
//    public Object listCollectByType(PmsFavorite productCollection) {
//        List<PmsFavorite> memberProductCollectionList = memberCollectionService.listProduct(memberService.getNewCurrentMember().getId(), productCollection.getType());
//        return new CommonResult().success(memberProductCollectionList);
//    }

	@ApiOperation("收藏列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "收藏类别：1商品  2文章", required = true, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
			@ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10")
	})
	@GetMapping(value = "collectList")
	public Object collectList(@RequestParam(value = "type", required = true) Integer type,
							  @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
							  @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
		if (ValidatorUtils.empty(type)) {
			return new CommonResult().failed("收藏类别不能为空");
		}
		UmsMember currentMember = memberService.getNewCurrentMember();
		if (type == 2) {
			IPage<UmsCollect> kepu = collectService.page(new Page<UmsCollect>(pageNum, pageSize), new QueryWrapper<UmsCollect>()
					.eq("member_id", currentMember.getId()).eq("type", 5).orderByDesc("create_time"));
			IPage<UmsCollect> huodong = collectService.page(new Page<UmsCollect>(pageNum, pageSize), new QueryWrapper<UmsCollect>()
					.eq("member_id", currentMember.getId()).eq("type", 6).orderByDesc("create_time"));
			IPage<UmsCollect> yanxue = collectService.page(new Page<UmsCollect>(pageNum, pageSize), new QueryWrapper<UmsCollect>()
					.eq("member_id", currentMember.getId()).eq("type", 7).orderByDesc("create_time"));
			IPage<UmsCollect> chuangke = collectService.page(new Page<UmsCollect>(pageNum, pageSize), new QueryWrapper<UmsCollect>()
					.eq("member_id", currentMember.getId()).eq("type", 8).orderByDesc("create_time"));
			Map map = new HashMap();
			map.put("kepu", kepu);
			map.put("huodong", huodong);
			map.put("yanxue", yanxue);
			map.put("chuangke", chuangke);
			return new CommonResult().success(map);
		}
		return new CommonResult()
				.success(collectService.page(new Page<UmsCollect>(pageNum, pageSize), new QueryWrapper<UmsCollect>()
						.eq("member_id", currentMember.getId()).eq("type", type).orderByDesc("create_time")));
	}

    @ApiOperation("收藏详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "productId", value = "商品或文章ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "type", value = "收藏类别：收藏类别：1->商品；2-4>活动商品；5->科普;6->活动；7->研学；8->创课；", required = true, dataType = "int", paramType = "query")
	})
    @GetMapping(value = "collectDetail")
    public Object collectDetail(@RequestParam(value = "productId", required = true) Long productId,
								@RequestParam(value = "type", required = true) Integer type) {
        UmsMember currentMember = memberService.getNewCurrentMember();
        return new CommonResult().success(collectService.getOne(
        		new QueryWrapper<UmsCollect>().eq("member_id", currentMember.getId()).eq("goods_id", productId).eq("type", type)));
    }

    @ApiOperation("点赞和取消点赞")
    @PostMapping("saveOrCancelFavorite")
    public Object saveOrCancelFavorite(CmsFavorite favorite) {
        return favoriteService.saveOrCancel(favorite);
    }

    @ApiOperation("点赞详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "objId", value = "商品或文章ID", required = true, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "type", value = "点赞类别：收藏类别：1->商品；2-4>活动商品；5->科普;6->活动；7->研学；8->创课；", required = true, dataType = "int", paramType = "query")
	})
    @GetMapping(value = "favoriteDetail")
    public Object favoriteDetail(@RequestParam(value = "objId", required = true) Long objId,
								 @RequestParam(value = "type", required = true) Integer type) {
        UmsMember currentMember = memberService.getNewCurrentMember();
        return new CommonResult().success(favoriteService.getOne(new QueryWrapper<CmsFavorite>().eq("member_id", currentMember.getId()).eq("obj_id", objId).eq("type", type)));
    }


//    @ApiOperation("删除点赞中的某个商品")
//    @PostMapping(value = "/deleteLike")
//    public Object deleteLike(String ids) {
//        if (StringUtils.isEmpty(ids)) {
//            return new CommonResult().failed("参数为空");
//        }
//        List<Long> resultList = new ArrayList<>(ids.split(",").length);
//        for (String s : ids.split(",")) {
//            resultList.add(Long.valueOf(s));
//        }
//
//        if (cmsFavoriteService.removeByIds(resultList)) {
//            return new CommonResult().success();
//        }
//        return new CommonResult().failed();
//    }

//    @ApiOperation("显示点赞列表")
//    @GetMapping(value = "/listLikeByType")
//    public Object listLikeByType(CmsFavorite productCollection) {
//        List<CmsFavorite> memberProductCollectionList = cmsFavoriteService.listProduct(memberService.getNewCurrentMember().getId(), productCollection.getType());
//        return new CommonResult().success(memberProductCollectionList);
//    }
//
//    @ApiOperation("显示点赞列表")
//    @GetMapping(value = "/listLike")
//    public Object listLike(CmsFavorite productCollection) {
//        List<CmsFavorite> memberProductCollectionList = cmsFavoriteService.listCollect(memberService.getNewCurrentMember().getId());
//        return new CommonResult().success(memberProductCollectionList);
//    }

	@ApiOperation("初始化是否点赞、收藏 ：1->商品；2-4>活动商品；5->科普;6->活动；7->研学；8->创课；")
	@GetMapping("init")
	public Object init(@RequestParam(value = "goodsId", required = true) Long goodsId,
			@RequestParam(value = "type", required = true) Integer type) {
		UmsMember currentMember = memberService.getNewCurrentMember();
		if (null == currentMember) {
			return new CommonResult().failed("请先登录");
		}
		if (ValidatorUtils.empty(goodsId)) {
			return new CommonResult().failed("ID不能为空");
		}
		if (ValidatorUtils.empty(type)) {
			return new CommonResult().failed("收藏类别不能为空");
		}
		Map<String, Object> map = collectService.init(goodsId, type, currentMember.getId());
		if (map != null) {
			return new CommonResult().success(map);
		} else {
			return new CommonResult().failed();
		}
	}
}
